﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UI.View.OwnerView
{
    /// <summary>
    /// Binding.xaml 的交互逻辑
    /// </summary>
    public partial class Binding : Page
    {
        ViewModel.OwnerViewModel vm;
        public Binding()
        {
            InitializeComponent();
            vm = DataContext as ViewModel.OwnerViewModel;

            vm.ListRooms = context.DB.Queryable<Model.Rooms>().ToList();

            vm.ListBuilds = context.DB.Queryable<Model.Building>().ToList();


            cmb_Build.ItemsSource = vm.ListBuilds;
            cmb_Build.DisplayMemberPath = "Name";
            cmb_Build.SelectedValuePath = "Id";
        }

        Model.DataContext context = new Model.DataContext();

        private void btn_Edit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_Delete_Clicl(object sender, RoutedEventArgs e)
        {
            if (vm.CurrRoom == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请选择房屋");
                return;
            }

            vm.CurrRoom.OwnerId = "";
            vm.CurrRoom.OwnerName = "";
            context.DB.Updateable<Model.Rooms>(vm.CurrRoom).ExecuteCommand();
            vm.ListRooms = context.DB.Queryable<Model.Rooms>().ToList(); ;
        }

        private void Btn_Binding_Click(object sender, RoutedEventArgs e)
        {
            if (vm.CurrRoom == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请选择房屋");
                return;
            }

            if(!string.IsNullOrEmpty( vm.CurrRoom.OwnerId))
            {
                HandyControl.Controls.Growl.WarningGlobal("该房屋已绑定！");
                return;
            }
            vm.Dialog =  HandyControl.Controls.Dialog.Show(new View.OwnerView.Part.SelectOwer());
        }

        private async void Btn_Query_Click(object sender, RoutedEventArgs e)
        {
            var Bid = 0; ;
            var No = txt_roomNo.Text;
            var name = txt_OwnerName.Text;
            var query = context.DB.Queryable<Model.Rooms>();
            if (cmb_Build.SelectedValue != null)
            {
                Bid = (int)cmb_Build.SelectedValue;
                query = query.Where(r => r.BuildingId == Bid);
            }

            if (!string.IsNullOrEmpty(No))
            {
                query = query.Where(b => b.No.Contains(No));
            }
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(b => b.OwnerName.Contains(Name));
            }



            vm.ListRooms = await query.ToListAsync();
        }
    }
}
